<?php //install Step 2 - Knowledgebase Details
define('DB_DNC', true); // do not connect to PDO DB
require_once('../inc/inc.common.php');

$tmpl 		= new mvTemplate();

// assign default values
$default_dbuser 	= mvSettings::get('db_user', 'root');
$default_dbpassword = mvSettings::get('db_password', '');
$default_dbhost 	= mvSettings::get('db_host', 'localhost');
$default_dbname 	= mvSettings::get('db_name', 'pomby');
$default_dbmaxrec 	= mvSettings::get('db_max_rec', '10');
$default_kbname 	= mvSettings::get('kbname', 'Pomby');
$default_kburl 		= mvSettings::get('kburl', 'http://localhost/pomby/');
$default_kbhomeurl 	= mvSettings::get('kbhomeurl', 'http://pomby.com/');

// unless we've come from somewhere else
if(isset($_POST['submit'])){
	$default_dbuser 	= $_POST['install_dbuser'];
	$default_dbpassword = $_POST['install_dbpassword'];
	$default_dbhost 	= $_POST['install_dbhost'];
	$default_dbname 	= $_POST['install_dbname'];
	$default_dbmaxrec 	= $_POST['install_dbmaxrec'];
}

$tmpl->assign('default_dbuser'		, $default_dbuser);
$tmpl->assign('default_dbpassword'	, $default_dbpassword);
$tmpl->assign('default_dbhost'		, $default_dbhost);
$tmpl->assign('default_dbname'		, $default_dbname);
$tmpl->assign('default_dbmaxrec'	, $default_dbmaxrec);
$tmpl->assign('default_kbname'		, $default_kbname);
$tmpl->assign('default_kburl'		, $default_kburl);
$tmpl->assign('default_kbhomeurl'	, $default_kbhomeurl);

// Check DB Connection
$db_pass = true;
$error = '';

if(!class_exists('mvPDO'))
	require_once(POMBY_DIR . '/classes/class.mvPDO.php');
if(!class_exists('mvPDOStatement'))
	require_once(POMBY_DIR . '/classes/class.mvPDOStatement.php');

try{
	$newdb = new mvPDO('mysql:host='.$default_dbhost, $default_dbuser, $default_dbpassword, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
	$newdb->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
	$newdb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mvPDOStatement', array()));

	// connected to db fine check if db exists
	if(!$newdb->query('USE `' . $default_dbname .'`')){
		if($newdb->query('CREATE DATABASE `' . $default_dbname.'`')){
			// database created successfully
			$newdb->query('USE `' . $default_dbname .'`');
		}else{
			$error = 'Connected to Database, however the database you selected does not exist and we are unable to create it.';
			$db_pass = false;
		}
	}
}catch(Exception $e){
	$error = 'Could not connect to database, please check your database login and password settings.  ' . print_r($e, true);
	$db_pass = false;
}

// Check if tables exist
$tables_exist = false;
if($db_pass){
	$sql = 'SHOW TABLES LIKE "authors"';
	if($query = $newdb->prepare_execute($sql)){
		if($query->fetch())
			$tables_exist = true;
	}else{
		$db_pass = false;
	}
}

// Insert tables if they exist
if($db_pass && !$tables_exist){
	$db_queries = explode(';', trim(file_get_contents(POMBY_DIR . '/docs/pomby.sql')));

	foreach($db_queries as $sql){
		if(trim($sql) != ''){
			if(!$newdb->query($sql)){
				$db_pass = false;
				$error = 'Unable to execute query: <pre>' . $sql . '</pre>';
			}
		}
	}

}

// write settings
if($db_pass){
	mvSettings::set('db_user', $default_dbuser);
	mvSettings::set('db_password', $default_dbpassword);
	mvSettings::set('db_host', $default_dbhost);
	mvSettings::set('db_name', $default_dbname);
	mvSettings::set('db_max_rec', $default_dbmaxrec);
	mvSettings::save(POMBY_DIR . '/config/config.php');
}

$tmpl->assign('db_pass', $db_pass);
$tmpl->assign('error', $error);
$tmpl->page_install('install_step2.tpl');

?>